package com.mf.config;

import cn.dev33.satoken.session.SaSession;
import cn.dev33.satoken.stp.StpInterface;
import cn.dev33.satoken.stp.StpUtil;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Luke on 2025/3/7 16:12.
 */
@Component
public class ConfigSaToken implements StpInterface {
    @Override
    public List<String> getPermissionList(Object loginId, String loginType) {
        return getStrList(StpUtil.getSession().getModel(SaSession.PERMISSION_LIST, List.class), loginId);
    }


    /**
     * 根据账号id查询角色集合
     *
     * @param loginId 账号id
     */
    @Override
    public List<String> getRoleList(Object loginId, String loginType) {
        return getStrList(StpUtil.getSession().getModel(SaSession.ROLE_LIST, List.class), loginId);
    }

    /**
     * 根据账号id查询权限集合
     */
    private static List<String> getStrList(Object permissions, Object loginId) {
//        if (Integer.valueOf(String.valueOf(loginId)).equals(ConstantPermission.ADMIN_ID)) {
//            return List.of("*");
//        }
        if (permissions instanceof List<?> tempList) {
            List<String> stringList = new ArrayList<>();
            for (Object obj : tempList) {
                if (obj instanceof String) {
                    stringList.add((String) obj);
                }
            }
            return stringList;
        }
        return new ArrayList<>();
    }
}
